package com.cgw.room.util;

public class BufferUtil {
    public static int readUInt8LE(byte[] bytes, int offset) {
        int res = 0;
        res = (bytes[offset] & 0xff);
        return res;
    }
    public static int readUInt16LE(byte[] bytes, int offset) {
        int res = 0;
        res = ((bytes[offset + 1] & 0xff) << 8) + (bytes[offset] & 0xff);
        return res;
    }
    public static int readInt32LE(byte[] bytes, int offset) {
        int res = 0;
        res = ((bytes[offset + 3] & 0xff) << 24) + ((bytes[offset + 2] & 0xff) << 16) + ((bytes[offset + 1] & 0xff) << 8) + (bytes[offset] & 0xff);
        return res;
    }
    public static double readDouble(byte[] bytes, int offset) {
        long value = 0;
        for (int i = 0; i < 8; i++) {
            value |= ((long) (bytes[i + offset] & 0xff)) << (8 * i);
        }
        return Double.longBitsToDouble(value);
    }

    public static void byteBuffer2FloatBuffer(byte[] bytes, float[] floats) {
        assert bytes.length == floats.length * 4;
        for (int n = 0; n < bytes.length; n += 4){
            int l;
            l = bytes[n + 0];
            l &= 0xff;
            l |= ((long) bytes[n + 1] << 8);
            l &= 0xffff;
            l |= ((long) bytes[n + 2] << 16);
            l &= 0xffffff;
            l |= ((long) bytes[n + 3] << 24);
            floats[n / 4] = Float.intBitsToFloat(l);
        }
    }

    public static void byteBuffer2ShortBuffer(byte[] bytes, short[] shorts) {
        assert bytes.length == shorts.length * 2;
        for (int n = 0; n < bytes.length; n += 2){
            short l;
            l = bytes[n + 0];
            l &= 0xff;
            l |= ((long) bytes[n + 1] << 8);
            shorts[n / 2] = l;
        }
    }

    public static float byte2float(byte[] b) {
        int l;
        l = b[0];
        l &= 0xff;
        l |= ((long) b[1] << 8);
        l &= 0xffff;
        l |= ((long) b[2] << 16);
        l &= 0xffffff;
        l |= ((long) b[3] << 24);
        return Float.intBitsToFloat(l);
    }
    public static float byte2float(byte[] b, int offset) {
        int l;
        l = b[0 + offset];
        l &= 0xff;
        l |= ((long) b[1 + offset] << 8);
        l &= 0xffff;
        l |= ((long) b[2 + offset] << 16);
        l &= 0xffffff;
        l |= ((long) b[3 + offset] << 24);
        return Float.intBitsToFloat(l);
    }
    public static byte[] float2byte(float f) {

        // 把float转换为byte[]
        int fbit = Float.floatToIntBits(f);

        byte[] b = new byte[4];
        for (int i = 0; i < 4; i++) {
            b[i] = (byte) (fbit >> (24 - i * 8));
        }

        // 翻转数组
        int len = b.length;
        // 建立一个与源数组元素类型相同的数组
        byte[] dest = new byte[len];
        // 为了防止修改源数组，将源数组拷贝一份副本
        System.arraycopy(b, 0, dest, 0, len);
        byte temp;
        // 将顺位第i个与倒数第i个交换
        for (int i = 0; i < len / 2; ++i) {
            temp = dest[i];
            dest[i] = dest[len - i - 1];
            dest[len - i - 1] = temp;
        }

        return dest;
    }
    public static byte[] int2byte_LittleEndian(int number){
        byte[] bytes = new byte[4]; // 因为int是32位的，需要4个byte

        bytes[0] = (byte)number;        // 取出最低8位
        bytes[1] = (byte)(number >> 8);  // 取出次低8位
        bytes[2] = (byte)(number >> 16); // 取出次高8位
        bytes[3] = (byte)(number >> 24); // 取出最高8位
        return bytes;
    }
}
